<?php
use Illuminate\Support\Facades\Response as FacadesResponse;
use Illuminate\Support\Facades\Event;

/**
 * @author gaozongwei
 */
class FollowController extends CenterController {
	
	public function follow() {
		$validation = $this->validateInput ();
		if (! is_null ( $validation )) {
			return $validation;
		}
		
		try {
			$follow = Follow::followed ( Input::get('follow_id') );
		} catch ( Exception $ex ) {
			Log::error ( $ex );
			return $this->makeView ( 'errors.503', '服务器暂时无法响应。请过一段时间以后再试。', 503 );
		}
//		Event::fire('lawyer_follow');   导致跨域报错
		return $this->makeView ( 'general.201', '成功关注', 201 );
	}
	
	public function followRemove() {
		$validation = $this->validateInput ();
		if (! is_null ( $validation )) {
			return $validation;
		}
		
		try {
			$follow = Follow::followCancle ( Input::get('follow_id') );
		} catch ( Exception $ex ) {
			Log::error ( $ex );
			return $this->makeView ( 'errors.503', '服务器暂时无法响应。请过一段时间以后再试。', 503 );
		}
		
		return $this->makeView ( 'general.200', '成功取消关注', 200 );
	}
	
	protected function validateInput() {
		$post = Input::all ();
		$rules = array ('follow_id' => 'required|exists:lawyer,user_id' );
		$messages = array ('follow_id.required' => '请选择用户', 'follow_id.exists' => '该用户不存在或者不是一个律师' );
		
		$validator = Validator::make ( $post, $rules, $messages );
		if ($validator->fails ()) {
			// the given data did not pass validation
			return $this->makeView ( 'errors.400', $validator->errors (), 400 );
		}
		return;
	}

}